package com.thunder.soft.ui.activity;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;

import com.gauravk.bubblenavigation.listener.BubbleNavigationChangeListener;
import com.thunder.soft.R;
import com.thunder.soft.common.base.BaseActivity;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.ActivityMainBinding;
import com.thunder.soft.ui.fragment.HomeFragment;
import com.thunder.soft.ui.fragment.PersonFragment;
import com.thunder.soft.ui.fragment.ProjectFragment;
import com.thunder.soft.ui.fragment.SystemFragment;
import com.thunder.soft.vm.MainViewModel;

import java.util.List;


public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private MainViewModel viewModel;
    private FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    private HomeFragment homeFragment;
    private ProjectFragment projectFragment;
    private SystemFragment systemFragment;
    private PersonFragment personFragment;

    @Override
    protected void initData() {
        viewModel = ViewModelProviderHelper.buildViewModel(this, MainViewModel.class);
        getLifecycle().addObserver(viewModel);

        viewModel.getFragmentLiveData().observe(this, new Observer<List<Fragment>>() {
            @Override
            public void onChanged(List<Fragment> fragments) {
                showFragment(0);
            }
        });

        binding.constrainView.setNavigationChangeListener(new BubbleNavigationChangeListener() {
            @Override
            public void onNavigationChanged(View view, int position) {
                showFragment(position);
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_main;
    }

    public void showFragment(int position) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment();

        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.container, homeFragment);
                } else {
                    fragmentTransaction.show(homeFragment);
                }
                break;
            case 1:
                if (projectFragment == null) {
                    projectFragment = new ProjectFragment();
                    fragmentTransaction.add(R.id.container, projectFragment);
                } else {
                    fragmentTransaction.show(projectFragment);
                }
                break;
            case 2:
                if (systemFragment == null) {
                    systemFragment = new SystemFragment();
                    fragmentTransaction.add(R.id.container, systemFragment);
                } else {
                    fragmentTransaction.show(systemFragment);
                }
                break;
            case 3:
                if (personFragment == null) {
                    personFragment = new PersonFragment();
                    fragmentTransaction.add(R.id.container, personFragment);
                } else {
                    fragmentTransaction.show(personFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    //隐藏Fragment
    private void hideFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (projectFragment != null) {
            fragmentTransaction.hide(projectFragment);
        }

        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }

        if (systemFragment != null) {
            fragmentTransaction.hide(systemFragment);
        }
        if (personFragment != null) {
            fragmentTransaction.hide(personFragment);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(viewModel);
    }
}